package A一周刷爆LeetCode.A基础班.I暴力递归;

/**
 * @author wei.zhao
 * @description: 拿纸牌
 * @date 2022年04月19日 14:07
 */
public class Code07_CardsInLine {

    public static void main(String[] args) {
        int[] arr = {1, 2, 100, 4,9,26,6,7,100,67,89,12435,78,65,88888,6574,112,654,77777};
        System.out.println(win(arr));
        System.out.println(f(arr, 0, arr.length - 1));
        System.out.println(s(arr,0,arr.length-1));
    }

    public static int win(int[] arr) {
        return Math.max(f(arr, 0, arr.length - 1), s(arr, 0, arr.length - 1));
    }

    public static int f(int[] arr, int l, int r) {
        if (l == r) {
            return arr[l];
        }
        return Math.max(arr[l] + s(arr, l + 1, r), arr[r] + s(arr, l, r - 1));
    }

    private static int s(int[] arr, int l, int r) {
        if (l == r) {
            return 0;
        }
        return Math.min(f(arr, l + 1, r), f(arr, l, r - 1));
    }

}
